﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RemoteLinqBase.ClientBase;
using RemoteLinqBase.Model;

namespace RemoteLinqClient
{
	class Program
	{
		static void Main(string[] args)
		{
			// remote url (replace ipv4.fiddler to localhost if not fiddler is not running!!!)
			const string remoteUri = //"http://localhost:59474/Service.svc";
            "http://ipv4.fiddler:59474/Service.svc";

			// register collections
			CollectionsFactory.RegisterCollection<Hotel>(remoteUri);
			CollectionsFactory.RegisterCollection<Room>(remoteUri);


            // test join
            Console.WriteLine("\ntest anonymous:");
            var roomJoinf = from room in LinqExtension.AsQueryable<Room>()
                           where room.HasBathroom
                           orderby room.Beds
                           select new { room.Name, room.Beds, room.Owner };
            foreach (var item in roomJoinf)
            {
                Console.WriteLine("Room {0} in {1} with {2} beds.", item.Name, item.Owner.Name, item.Beds);
            }

			// test hotel select
			Console.WriteLine("\ntest hotel select:");
			var items = from hotel in LinqExtension.AsQueryable<Hotel>()
						select hotel;
			foreach (var item in items)
			{
				Console.WriteLine("Hotel {0} with {1} rooms.", item.Name, item.Rooms.Length);
			}

			// test hotel count
			Console.WriteLine("\ntest hotel count");
			var count = (from hotel in LinqExtension.AsQueryable<Hotel>()
						select hotel).Count();
			Console.WriteLine("Number of hotels {0}", count);

			// test hotel where 1
			Console.WriteLine("\ntest hotel where 1");
			items = from hotel in LinqExtension.AsQueryable<Hotel>()
						where hotel.Rooms.Any(room => room.HasBathroom)
						select hotel;
			foreach (var item in items)
			{
				Console.WriteLine("Hotel {0} with bathroom.", item.Name);
			}

			// test hotel where 2
			Console.WriteLine("\ntest hotel where 2");
			items = from hotel in LinqExtension.AsQueryable<Hotel>()
					where hotel.Name.Contains("Hotel")
					select hotel;
			foreach (var item in items)
			{
				Console.WriteLine("Hotel {0} with 'Hotel' in name.", item.Name);
			}

			// test rooms select
			Console.WriteLine("\ntest room select:");
			var rooms = from room in LinqExtension.AsQueryable<Room>()
						select room;
			foreach (var item in rooms)
			{
				Console.WriteLine("Room {0} in {1} with {2} beds and bath [{3}] and wc [{4}].", item.Name, item.Owner.Name, item.Beds, item.HasBathroom, item.HasToilet);
			}

			// test rooms order by bed count
			Console.WriteLine("\ntest order by bed count:");
			rooms = from room in LinqExtension.AsQueryable<Room>()
					orderby room.Beds
					select room;
			foreach (var item in rooms)
			{
				Console.WriteLine("Room {0} in {1} with {2} beds and bath [{3}] and wc [{4}].", item.Name, item.Owner.Name, item.Beds, item.HasBathroom, item.HasToilet);
			}

			// test rooms order by bed count only with bathroom
			Console.WriteLine("\ntest order by bed count  only with bathroom:");
			rooms = from room in LinqExtension.AsQueryable<Room>()
					where room.HasBathroom
					orderby room.Beds
					select room;
			foreach (var item in rooms)
			{
				Console.WriteLine("Room {0} in {1} with {2} beds and bath [{3}] and wc [{4}].", item.Name, item.Owner.Name, item.Beds, item.HasBathroom, item.HasToilet);
			}

			// test paging with skipping 3 items and taking 3 items
			Console.WriteLine("\ntest paging with skipping 3 items and taking 3 items:");
			rooms = (from room in LinqExtension.AsQueryable<Room>()
					select room).Skip(3).Take(3);
			foreach (var item in rooms)
			{
				Console.WriteLine("Room {0} in {1} with {2} beds and bath [{3}] and wc [{4}].", item.Name, item.Owner.Name, item.Beds, item.HasBathroom, item.HasToilet);
			}

            // test first and last
            Console.WriteLine("\ntest first and last:");
            var roomFirst = (from room in LinqExtension.AsQueryable<Room>()
                     select room).First();
            var roomLast = (from room in LinqExtension.AsQueryable<Room>()
                             select room).Last();
            Console.WriteLine("First Room {0} in {1} with {2} beds and bath [{3}] and wc [{4}].", roomFirst.Name, roomFirst.Owner.Name, roomFirst.Beds, roomFirst.HasBathroom, roomFirst.HasToilet);
            Console.WriteLine("Last Room {0} in {1} with {2} beds and bath [{3}] and wc [{4}].", roomLast.Name, roomLast.Owner.Name, roomLast.Beds, roomLast.HasBathroom, roomLast.HasToilet);
            

			// test join
			Console.WriteLine("\ntest anonymous:");
            var roomJoin = from room in LinqExtension.AsQueryable<Room>()
                           where room.HasBathroom
                           orderby room.Beds
                           select new { room.Beds, room.Owner.Name };
            foreach (var item in roomJoin)
			{
				Console.WriteLine("Room {0} in {1} .", item.Name, item.Beds);
			}

			Console.Read();

		}
	}
}
